package com.example.electronicmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.electronicmall.dao.ShopDao;
import com.example.electronicmall.entity.BusinessShopEntity;
import com.example.electronicmall.entity.ItemEntity;
import com.example.electronicmall.entity.ItemShopEntity;
import com.example.electronicmall.service.BusinessShopService;
import com.example.electronicmall.service.ItemService;
import com.example.electronicmall.service.ItemShopService;
import com.example.electronicmall.service.ShopService;
import com.example.electronicmall.utils.PageUtils;
import com.example.electronicmall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.electronicmall.entity.ShopEntity;


@Service("shopService")
public class ShopServiceImpl extends ServiceImpl<ShopDao, ShopEntity> implements ShopService {

    @Autowired
    BusinessShopService businessShopService;

    @Autowired
    ItemShopService itemShopService;

    @Autowired
    ItemService itemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }

    @Override
    public void createShop(Integer businessId, String shopName, String shopAddress) {
        ShopEntity shop = new ShopEntity();
        shop.setShopName(shopName);
        shop.setShopAddress(shopAddress);
        shop.setCreatedAt(new Date());
        shop.setUpdatedAt(new Date());
        this.baseMapper.insert(shop);

        Integer shopId = this.baseMapper.selectOne(new QueryWrapper<ShopEntity>().eq("shop_name", shopName)).getShopId();
        BusinessShopEntity bs = new BusinessShopEntity();
        bs.setBusinessId(businessId);
        bs.setShopId(shopId);
        businessShopService.getBaseMapper().insert(bs);
    }

    @Override
    public List<ShopEntity> queryShopByBusinessId(Integer businessId) {
        List<BusinessShopEntity> bsList = businessShopService.getBaseMapper().selectList(new QueryWrapper<BusinessShopEntity>().eq("business_id", businessId));
        return bsList.stream().map((v) -> this.baseMapper.selectOne(new QueryWrapper<ShopEntity>().eq("shop_id", v.getShopId()))).collect(Collectors.toList());
    }

    @Override
    public List<ShopEntity> listAllShops() {
        return this.baseMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<ShopEntity> listShopsByBusinessId(Integer businessId) {
        List<BusinessShopEntity> bsList = businessShopService.getBaseMapper().selectList(new QueryWrapper<BusinessShopEntity>().eq("business_id", businessId));
        List<Integer> collect = bsList.stream().map(BusinessShopEntity::getShopId).collect(Collectors.toList());
        List<ShopEntity> list = new ArrayList<>();
        for(int shopId : collect){
            list.add(this.baseMapper.selectOne(new QueryWrapper<ShopEntity>().eq("shop_id", shopId)));
        }
        return list;
    }
}